<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('字典类型列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">字典名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="searchText" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dictType-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dictType-table" lay-filter="dictType-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="role-toolbar">
    <button shiro:hasPermission="system:dictType:add" class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button shiro:hasPermission="system:dictType:remove" class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="dictType-bar">
    <button class="pear-btn pear-btn-warming pear-btn-md" lay-event="view" title="查看"><i class="layui-icon layui-icon-search"></i>
    </button>

    <button shiro:hasPermission="system:dictType:edit" class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="修改"><i class="layui-icon layui-icon-edit"></i>
    </button>

    <button shiro:hasPermission="system:dictType:remove" class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" title="删除"><i class="layui-icon layui-icon-delete"></i>
    </button>

</script>

<script type="text/html" id="dictType-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="dictType-enable" {{ d.status== 0 ? 'checked' : '' }}>
</script>



<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let MODULE_PATH = rootPath+"/DictTypeController";
        let DICTDATA_PATH = rootPath+"/DictDataController";
        let cols = [
            [
                {type: 'checkbox'},
                {title: 'ID', field: 'id', align: 'center'},
                {title: '字典名称', field: 'dictName', align: 'left'},
                {title: '字典类型', field: 'dictType', align: 'left'},
                {title: '状态', field: 'status', align: 'center',toolbar: '#dictType-enable'},
                {title: '创建者', field: 'createBy', align: 'left'},
                {title: '创建时间', field: 'createTime', align: 'left'},
                {title: '备注', field: 'remark', align: 'left'},
                {title: '操作', toolbar: '#dictType-bar', align: 'center'}
            ]
        ]

        //table表单列表
        table.render({
            elem: '#dictType-table',
            url: MODULE_PATH + '/list',
            page: true,
            method: 'get',
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        //table图标事件绑定
        table.on('tool(dictType-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }else if (obj.event === 'view') {
                window.view(obj);
            }
        });

        //table工具栏绑定
        table.on('toolbar(dictType-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        //查询按钮绑定
        form.on('submit(dictType-query)', function (data) {
            table.reload('dictType-table', {where: data.field})
            return false;
        });


        //添加
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + '/add'
            });
        }
        //修改
        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '500px'],
                content: MODULE_PATH + '/edit/' + obj.data['id']
            });
        }

        //查看字典数据
        window.view = function (obj) {
            window.location.href=DICTDATA_PATH + "/view?dictId=" + obj.data['id'];
        }

        //删除
        window.remove = function (obj) {
            layer.confirm('确定要删除该字典类型', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "/remove?ids=" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code==200) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        //批量删除
        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些字典类型', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "/remove?ids=" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code==200) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('dictType-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        //状态切换绑定
        form.on('switch(dictType-enable)', function (obj) {
            let operate;

            if (obj.elem.checked) {
                operate = '0';
            } else {
                operate = '1';
            }

            let loading = layer.load()

            $.ajax({
                url: MODULE_PATH+'/updateEnable',
                data: JSON.stringify({id: this.value,status:operate}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.code==200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
        });


        //刷新按钮
        window.refresh = function () {
            table.reload('dictType-table');
        }
    })
</script>
</html>